import * as API from '../../../api/shareApi';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    hiddenPrice: false,
    customName: '',
    companyName: '',
    latestNodeList: [],
    // allRead: true,
    shareId: '',
    userId: '',
    formShare: false,
    order: {
      deliverStatus: ''
    },
    custom: {},
    isFromShareCard: false,
    extra_data: {
      key: '',
      shareId: ''
      // shareInfo: {
      //   customId: '',
      //   shardingId: '',
      //   companyName: '',
      //   userId: '',
      //   sharePage: 'shareDetail',
      //   orderId: '',
      //   shareId: '',
      //   customName: ''
      // }
    },
    navigator_1: {
      title: '快速体验',
      tag: '企业端'
    }
  },
  setKey: function () {
    setTimeout(() => {
      this.setData({
        'extra_data.key': new Date().getTime()
      });
    }, 100)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shareId) {
      this.setData({
        'extra_data.shareId': options.shareId
      });
      API.getOrdersByShareId({ shareId: options.shareId }).then(res => {
        // let shareBody = 'extra_data.shareInfo';
        // let shareInfo = {
        //   companyName: res.companyName,
        //   shareId: options.shareId,
        //   shardingId: res.orderList && res.orderList[0].shardingId,
        //   customId: res.orderList && res.orderList[0].customId,
        //   customName: res.orderList && res.orderList[0].customName,
        //   userId: res.shareContent && res.shareContent.userId,
        //   sharePage: 'shareDetail'
        // }
        if (res.orderList) {
          this.setData({
            orderId: res.orderList[0].id,
            hiddenPrice: res.orderList[0].hiddenPrice + '' == 'false' ? false : true,
            customName: res.orderList && res.orderList[0].customName,
            isFromShareCard: true
            // [shareBody]: shareInfo
          })
          this.getData(res.orderList[0].id)
        }
      }, err => {
        if (err == '已过期') {
          wx.redirectTo({
            url: '../../expire/expire'
          })
        };
      })
    } else {
      this.setData({
        isFromShareCard: false,
        orderId: options.id,
        hiddenPrice: options.hiddenPrice + '' == 'false' ? false : true
      })
      wx.setNavigationBarTitle({
        title: '订单详情'
      })
      this.getData(options.id);    //从分享列表进入
    }

  },
  getData: function (id) {
    API.getShareOrderDetail({ orderId: id }).then(res => {
      if (!res) return;
      let data = {
        order: res.order,
        custom: res.custom,
        nodeEventMap: res.nodeEventMap,
        nodes: {},
        companyName: res.company && res.company.name,
        companyId: res.company && res.company.id,
        latestNodeList: res.latestNodeList
      };

      this.setData(data);
    }, err => {
      util.toast('读取订单数据失败，请稍后再试');
      setTimeout(() => {
        wx.navigateBack({ delta: 1 })
      }, 1500);
    })
  },
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: this.data.order.salesmanTel
    })
  },
  gotoProcess: function () {
    wx.navigateTo({
      url: '/pages/share/shareProcess/shareProcess?orderId=' + this.data.orderId + '&hiddenPrice=' + this.data.hiddenPrice
    })
  },
  gotoHome: function () {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },
  gotoLogin: function () {
    let token = wx.getStorageSync('token');
    if (token) {
      wx.redirectTo({
        url: '/pages/home/home',
      })
    } else {
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'extra_data.key': new Date().getTime()
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})